package cn.etgps.etgps.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import cn.etgps.etgps.R;

public class SearchView extends LinearLayout implements OnClickListener, OnEditorActionListener {
    public boolean isShowInTitle = true;//是否显示搜索框
    private EditText mEditText = null;//搜索框内容
    private ImageView mSearchBtn = null;//放大镜图标
    private ImageView mClearBtn = null;//叉叉图标
    private boolean isQuickSearch = false;//是否开启快速搜索
    private InputMethodManager mInputManager = null;//系统软键盘
    private SearchListener mSearchListener = null;//搜索监听
    //编辑框监听器
    TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {


        }

        @Override
        public void onTextChanged(CharSequence str, int start, int before, int count) {
            if (str.length() > 0) {
                mClearBtn.setVisibility(View.VISIBLE);
                mSearchBtn.setVisibility(View.GONE);
                if (isQuickSearch && mSearchListener != null && (str.length() % 2 == 0)) {
                    mSearchListener.doSearch(str.toString());
                }
            } else {
                mClearBtn.setVisibility(View.GONE);
                mSearchBtn.setVisibility(View.VISIBLE);
                mSearchListener.doSearch(str.toString());
            }
        }
    };
    private Context mContext = null;

    public SearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    public SearchView(Context context) {
        super(context);
        mContext = context;
    }

    //titleBar中getSearchView后使用initSearchView初始化搜索栏
    public void initSearchView(SearchListener listener, boolean quickSearch) {
        isQuickSearch = quickSearch;
        mSearchListener = listener;
        initView(mContext);
    }

    //根据searchview的位置解析不同的xml，添加到界面中并初始化控件及监听
    private void initView(Context context) {
        LayoutInflater mInflater = LayoutInflater.from(context);
        View view = null;
        if (isShowInTitle) {
            view = mInflater.inflate(R.layout.searchbar_title, null);
        } else {
            view = mInflater.inflate(R.layout.searchbar_view, null);
        }
        this.addView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        mEditText = (EditText) view.findViewById(R.id.inputbox);
        mEditText.addTextChangedListener(mTextWatcher);//编辑框监听器
        //系统软键盘
        mInputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        mSearchBtn = (ImageView) view.findViewById(R.id.search);
        mSearchBtn.setOnClickListener(this);
        mClearBtn = (ImageView) view.findViewById(R.id.close);
        mClearBtn.setOnClickListener(this);
        mEditText.setOnEditorActionListener(this);//搜索键监听
    }

    public void setSearchText(String key) {
        mEditText.setText(key);
    }

    //开始搜索按钮显示
    public void showSearchView() {
        mSearchBtn.setVisibility(View.VISIBLE);
        mClearBtn.setVisibility(View.GONE);
        mEditText.setText("");
        mEditText.requestFocus();
        showSoftInput();
    }

    //搜索栏取消按钮
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.close) {
            mEditText.setText("");
            mSearchBtn.setVisibility(View.VISIBLE);
            mClearBtn.setVisibility(View.GONE);
            hideSoftInput(mEditText);
            mEditText.clearFocus();
            if (mSearchListener != null) {
                mSearchListener.doSearch("");
            }
        }
    }

    //获取用户输入的信息
    public String getInput() {
        return mEditText.getText().toString().trim();
    }

    //actionId,xml中editText有属性android:imeOptions="actionSearch"
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            hideSoftInput(mEditText);
            String key = mEditText.getText().toString();
            //do search
            if (mSearchListener != null) {
                mSearchListener.doSearch(key);
            }
            return true;
        }
        return false;
    }

    /**
     * Hide the soft input
     */
    public void hideSoftInput(View view)//隐藏软键盘
    {
        mInputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    /**
     * Show the soft input
     */
    public void showSoftInput()//显示软键盘
    {
        mInputManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }

    public interface SearchListener {
        public void doSearch(String key);
    }
}
